package com.icygen.eclipsetools.templates;


import java.io.*;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.CoreException;

import com.icygen.core.Utils;

public class Template {
	
	protected String _title;
	
	/**
     * Constructor
     */
    public Template(String title) {
    	_title = title;
    }
    
    /**
     * Initialize the file contents to contents of the given resource.
     */
    public InputStream openContentStream()  throws CoreException
    {

        final String newline = "\n"; // System.getProperty("line.separator");
        String line;
        StringBuffer sb = new StringBuffer();
        try {
            InputStream input = this.getClass().getResourceAsStream("/resources/" + _title + ".tpl");
            
            /* */
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            try {
            	
                while ((line = reader.readLine()) != null) {
                    line = line.replaceAll("\\$\\{title\\}", _title);
                    sb.append(line);
                    sb.append(newline);
                }	
            } finally {
                reader.close();
            }
        
        } catch (IOException ioe) {
            IStatus status = new Status(IStatus.ERROR, "ExampleWizard", IStatus.OK,
                    ioe.getLocalizedMessage(), null);
            throw new CoreException(status);
        }
         
        return new ByteArrayInputStream(sb.toString().getBytes());
        

    }

}
